import asunit.framework.TestCase;

import mockAS2.MockAS2;
import mockAS2.Sandbox;
import mockAS2.SpyMethod;
import mockAS2.SpyObject;
import mockAS2.Stub;
import mockAS2.mocks.ClassToBeMocked;

/**
 * Copyright (c) 2012 Gary Higham
 * Released under the MIT license
 * https://code.google.com/p/mock-as2/wiki/License
 *
 * Version: v0.1
 */
class mockAS2.MockAS2Test extends TestCase {
	private var className : String = "mockAS2.MockAS2Test";
	
	public function MockAS2Test(testMethod : String) {
		super(testMethod);
	}
	
	public function testCanCreateSandbox() : Void {
		var sandbox : Sandbox = MockAS2.createSandbox();
		assertTrue("Valid sandbox has been created", sandbox instanceof Sandbox);
	}
	
	public function testGetSandboxReturnsSameSandboxAsCreate() : Void {
		var sandbox : Sandbox = MockAS2.createSandbox();
		assertEquals("Sandboxes created is equal to sandbox form getter", sandbox, MockAS2.getSandbox());
	}
	
	public function testSandboxRestore() : Void {
		var sandbox : Sandbox = MockAS2.createSandbox();
		var spy : SpyMethod = new SpyMethod(sandbox, "restore");
		MockAS2.restoreSandbox();
		assertTrue("Sandbox has been restored", spy.calledOnce());
	}
	
	public function testCanCreateStub() : Void {
		assertTrue("Valid stub has been created", createStub() instanceof Stub);
	}
	
	public function testStubIsAddedToSandbox() : Void {
		var spy : SpyMethod = spyOnSandboxAddItem();
		var stub : Stub = createStub();
		assertTrue("Stub should have been added to Sandbox", spy.calledWithExactArgs(stub));
	}
	
	public function testCanCreateSpyObject() : Void {
		assertTrue("Valid Object Spy has been created", MockAS2.createSpyOnObject(null, null) instanceof SpyObject);
	}
	
	public function testSpyObjectIsAddedToSandbox() : Void {
		var spy : SpyMethod = spyOnSandboxAddItem();
		var objectUnderSpy : SpyObject = MockAS2.createSpyOnObject(null, null);
		assertTrue("SpyObject should have been added to Sandbox", spy.calledWithExactArgs(objectUnderSpy));
	}
	
	public function testCanCreateSpyMethod() : Void {
		assertTrue("Valid Spy method has been created", MockAS2.createSpyOnMethod() instanceof SpyMethod);
	}
	
	public function testSpyMethodIsAddedToSandbox() : Void {
		var spy : SpyMethod = spyOnSandboxAddItem();
		var methodUnderSpy : SpyMethod = MockAS2.createSpyOnMethod();
		assertTrue("SpyMethod should have been added to Sandbox", spy.calledWithExactArgs(methodUnderSpy));
	}
	
	private function createStub() : Stub {
		var dummy : ClassToBeMocked = new ClassToBeMocked();
		return MockAS2.createStub(dummy, "return1", null);
	}
	
	private function spyOnSandboxAddItem() : SpyMethod {
		var sandbox : Sandbox = MockAS2.createSandbox();
		return new SpyMethod(sandbox, "addItem");
	}
}
